package cn.iocoder.yudao.module.iot.controller.app.operationhistory;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.iot.controller.admin.operationhistory.vo.OperationHistoryPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.operationhistory.vo.OperationHistoryRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.operationhistory.vo.OperationHistorySaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.operationhistory.OperationHistoryDO;
import cn.iocoder.yudao.module.iot.service.operationhistory.OperationHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 操作历史记录")
@RestController
@RequestMapping("/iot/operation-history")
@Validated
@Slf4j
public class AppOperationHistoryController {

    @Resource
    private OperationHistoryService operationHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建操作历史记录")
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:create')")
    public CommonResult<Long> createOperationHistory(@Valid @RequestBody OperationHistorySaveReqVO createReqVO) {
        return success(operationHistoryService.createOperationHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新操作历史记录")
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:update')")
    public CommonResult<Boolean> updateOperationHistory(@Valid @RequestBody OperationHistorySaveReqVO updateReqVO) {
        operationHistoryService.updateOperationHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除操作历史记录")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:delete')")
    public CommonResult<Boolean> deleteOperationHistory(@RequestParam("id") Long id) {
        operationHistoryService.deleteOperationHistory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除操作历史记录")
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:delete')")
    public CommonResult<Boolean> deleteOperationHistoryList(@RequestParam("ids") List<Long> ids) {
        operationHistoryService.deleteOperationHistoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作历史记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:query')")
    public CommonResult<OperationHistoryRespVO> getOperationHistory(@RequestParam("id") Long id) {
        OperationHistoryDO operationHistory = operationHistoryService.getOperationHistory(id);
        return success(BeanUtils.toBean(operationHistory, OperationHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得操作历史记录分页")
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:query')")
    public CommonResult<PageResult<OperationHistoryRespVO>> getOperationHistoryPage(@Valid OperationHistoryPageReqVO pageReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();

        if (userId == null) {
            log.warn("用户未登录，操作失败");
            return CommonResult.error(401, "用户未登录");
        }
        PageResult<OperationHistoryDO> pageResult = operationHistoryService.getOperationHistoryPage(pageReqVO, userId);
        return success(BeanUtils.toBean(pageResult, OperationHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出操作历史记录 Excel")
    //@PreAuthorize("@ss.hasPermission('iot:operation-history:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOperationHistoryExcel(@Valid OperationHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        List<OperationHistoryDO> list = operationHistoryService.getOperationHistoryPage(pageReqVO, userId).getList();
        // 导出 Excel
        ExcelUtils.write(response, "操作历史记录.xls", "数据", OperationHistoryRespVO.class,
                        BeanUtils.toBean(list, OperationHistoryRespVO.class));
    }

}